package LeetCode;

public class LC_005_LongestPalindromicSubstring {
    public static void main(String[] args) {
        String s = "123321";
        System.out.println(longestPalindrome(s));
    }

    private static String longestPalindrome(String s) {
        String result = null;
        int max = 0;
        for (int i = 0; i < s.length(); i++) {
            int st = i;
            int en = i + 1;
            while (st >= 0 && en <= s.length() - 1 && s.charAt(st) == s.charAt(en)) {
                st--;
                en++;
            }
            if (en - st - 1 > max) {
                max = en - (st + 1);
                result = s.substring(st + 1, en - 1 + 1);
            }

            st = i - 1;
            en = i + 1;
            while (st >= 0 && en <= s.length() - 1 && s.charAt(st) == s.charAt(en)) {
                st--;
                en++;
            }
            if (en - st - 1 > max) {
                max = en - st - 1;
                result = s.substring(st + 1, en - 1 + 1);
            }
        }
        return result;
    }
}
